package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.Permission;
import com.sdry.model.llm.Role;
import com.sdry.service.llm.RoleService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 角色
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:48:49
*/

@Controller
@RequestMapping("/roleMana")
public class RoleController {

	@Resource
	private RoleService roleService;  //注入角色service
	
	/**
	 * 分页查询角色信息
	 * @param page 分页条件
	 * @param role 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageRole")
	public void listPageRole(Page page, Role role, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (role.getName() != null ) {
			map.put("name", role.getName());
		} else {
			map.put("name", "");
			role.setName("");
		}
		int count = roleService.countRole(role);
		List<Role> list = roleService.listPageRole(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存角色信息
	 * @param role 角色信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveRole")
	public void saveRole(Role role, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (role.getRid() == null || "".equals(role.getRid())) {
			isSuccess = roleService.insertRole(role);
		} else {
			isSuccess = roleService.updateRole(role);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除角色
	 * @param str rid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteRole")
	public void deleteRole(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = roleService.deleteRole(str);
		ResponseUtil.write(response, isSuccess);
		
	}
	
	/**
	 * 校验是否删除角色
	 * @param str rid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteRoleOfVerify")
	public void deleteRoleOfVerify(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = roleService.deleteRoleOfVerify(str);
		ResponseUtil.write(response, isSuccess);
		
	}
	
	/**
	 * 根据角色ID获取权限
	 * @param rid 角色ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getPermissionByRole")
	public void getPermissionByRole(@RequestParam("rid") String rid, HttpServletResponse response) throws Exception{
		List<Permission> permissions = roleService.getPermissionByRole(Long.parseLong(rid));
		//将对象转化为json数组
        JSONArray json = JSONArray.fromObject(permissions);
		ResponseUtil.write(response, json);
	}
	
	/**
	 * 角色授权
	 * @param pids 权限ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/accreditRole")
	public void accreditUser(@RequestParam("ztree") String pids,@RequestParam("rid") String rid, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = roleService.accreditUser(pids, rid);
		ResponseUtil.write(response, isSuccess);
		
	}
	
	/**
	 * 所有权限
	 * @param rid 角色ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getAllPermission")
	public void getPermissionByRole(HttpServletResponse response) throws Exception{
		List<Permission> permissions = roleService.findAllPermission();
		//将对象转化为json数组
        JSONArray json = JSONArray.fromObject(permissions);
		ResponseUtil.write(response, json);
	}
	
}
